$package('itsm.news');

itsm.news.categoryMain=function(){
	this.opt='';
	return {
		categoryCodeForma:function(cell){
			if(cell=='NewNotice')
				return '最新公告';
			if(cell=='IndustryNews')
				return '行业新闻';
			if(cell=='NewDynamic')
				return '最新动态';
			if(cell=='CompanyProfile')
				return '公司简介';
			if(cell=='CorporateCulture')
				return '企业文化';
			if(cell=='Wealthprogram')
				return '财富方案';
			return cell;
		},
		//列表
		categoryMainGrid:function(){
			var params = $.extend({},jqGridParams, {	
				url:'category!findCategoryPager.action',
				caption:'分类列表',
				colNames:['ID','分类名称','分类编码','排序','分类规则','描述','栏目'],
			 	colModel:[
			 	          {name:'categoryId',align:'center'},
			 	          {name:'categoryName',align:'left'},
			 	          {name:'categoryName',align:'left'},
			 	          {name:'categoryOrder',align:'left'},
			 	         {name:'categoryCode',align:'left',formatter:itsm.news.categoryMain.categoryCodeForma},
			 	          {name:'description',align:'left'},
			 	         {name:'topicName',align:'left'}
			 	],
				jsonReader: $.extend(jqGridJsonReader, {id: "categoryId"}),
				sortname:'categoryId',
				pager:'#categoryMainPager'
				});
				$("#categoryMainGrid").jqGrid(params);
				$("#categoryMainGrid").navGrid('#categoryMainPager',navGridParams);
				//列表操作项
				$("#t_categoryMainGrid").css(jqGridTopStyles);
				$("#t_categoryMainGrid").html($('#categoryMainGridToolbar').html());
				
				//自适应宽度
				setGridWidth("#categoryMainGrid","regCenter",10);
		},
		//加载栏目
		loadTopicOption:function(select,type){
			$.post('topic!findTopicPager.action','rows=10000',function(result){
				$(select).html('');
				var data = result.data;
				if(type=='selectNull'){
					$('<option value="">--请选择-- </option>').appendTo(select);
				}
				for(var i=0;i<data.length;i++){
					$('<option value="'+data[i].topicId+'">'+data[i].topicName+'</option>').appendTo(select);
				}
					
			});
		},
		//打开添加窗口
		openAddWin:function(){
			opt='saveCategory';
			resetForm('#categoryMain_add_form');
			$('#categoryMain_add_win').window('open');
		},
		
		//打开编辑窗口
		openEditWin:function(){
			
			opt='editCategory';
			checkBeforeEditGrid('#categoryMainGrid',function(rowData){
				$.post('category!findCategoryInfo.action','categoryDTO.categoryId='+rowData.categoryId,function(data){
					$('#categoryMain_categoryId').val(data.categoryId);
					$('#categoryMain_categoryName').val(data.categoryName);
					$('#categoryMain_categoryOrder').val(data.categoryOrder);
					$('#categoryMain_description').val(data.description);
					$('#categoryMain_categoryCode').val(data.categoryCode);
					$('#categoryMain_topicId').val(data.topicId);
				});
				$('#categoryMain_add_win').window('open');
			});	
		},
		
		//保存
		saveCategory:function(){
			if($('#categoryMain_add_win').form('validate')){
				var _param = $('#categoryMain_add_win form').serialize();
				$.post('category!'+opt+'.action',_param,function(data){
					msgShow('保存成功!','show');
					$('#categoryMain_add_win').window('close');
					$('#categoryMainGrid').trigger('reloadGrid');
				})
				
			}
		},
		//删除
		deleteCategory:function(){
			checkBeforeDeleteGrid('#categoryMainGrid',function(rowIds){
				var pp = $.param({'ids':rowIds},true);
				$.post("category!deleteCategory.action", pp, function(){
					$('#categoryMainGrid').trigger('reloadGrid');
					msgShow('删除成功!','show');
					
				}, "json");
			});
		},
		//打开搜索窗口
		openSearchWin:function(){
			$('#categoryMain_search_win').window('open');
		},
		//搜索
		searchCategory:function(){
			
			var sdata=$('#categoryMain_search_win form').getForm();
			var postData = $("#categoryMainGrid").jqGrid("getGridParam", "postData");
			$.extend(postData,sdata);
			var _url = 'category!findCategoryPager.action';	
			$('#categoryMainGrid').jqGrid('setGridParam',{url:_url}).trigger('reloadGrid',[{"page":"1"}]);
		},
		init:function(){
			itsm.news.categoryMain.loadTopicOption('#categoryMain_topicId','');
			itsm.news.categoryMain.loadTopicOption('#categoryMainSearch_topicId','selectNull');
			itsm.news.categoryMain.categoryMainGrid();
		}
	}
	
}();

$(function(){
	
	itsm.news.categoryMain.init();
	
});